<template>
  <panel>

    <!--列表搜索 复制-->
    <div class="search-box">
      <el-row class="form-horizontal">
        <el-col :span="12">
          <router-link to="/demo/add"><el-button type="info" size="small">新增</el-button></router-link>
          <el-button type="info" size="small">编辑</el-button>
          <el-button type="danger" size="small">删除</el-button>
          <!--<el-button type="primary" size="small">上传<i class="el-icon-upload el-icon&#45;&#45;right"></i></el-button>-->
          <el-button type="info" size="small" @click="handleDownload">导出</el-button>
        </el-col>
        <el-col :span="12" class="text-right">
          <el-button :plain="true" type="info" size="small" class="right" @click="searchState = !searchState">高级搜索</el-button>
          <el-col :span="16" class="right mr15">
            <el-input
              size="small"
              placeholder="请输入内容"
              icon="search"
              v-model="input"
              :on-icon-click="handleIconClick">
            </el-input>
          </el-col>
        </el-col>
      </el-row>

      <!--高级搜索-->
      <el-row class="collapseBox" v-show="searchState">
        <el-col :span="24" class="title">
          高级搜索
        </el-col>
        <el-col :span="24" class="form-wrap">
          <!--:rules="rules" 验证-->
          <el-form :model="ruleForm" ref="ruleForm" label-width="100px" :label-position="'left'">
            <el-form-item label="活动名称" prop="name" class="item-5">
              <el-input v-model="ruleForm.name" size="small"></el-input>
            </el-form-item>
            <el-form-item label="活动区域" prop="region" class="item-5">
              <el-select v-model="ruleForm.region" size="small" placeholder="请选择活动区域">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="活动时间" prop="date1" class="item-5">
                <el-date-picker type="date" placeholder="选择日期" size="small" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item label="活动时间" prop="date2" class="item-5">
              <el-date-picker type="date" placeholder="选择日期" size="small" v-model="ruleForm.date2" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item label="活动性质" prop="type" class="item-5">
              <el-checkbox-group v-model="ruleForm.type">
                <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
                <el-checkbox label="地推活动" name="type"></el-checkbox>
                <el-checkbox label="线下主题活动" name="type"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="特殊资源" prop="resource" class="item-5">
              <el-radio-group v-model="ruleForm.resource">
                <el-radio label="线上品牌商赞助"></el-radio>
                <el-radio label="线下场地免费"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="即时配送" prop="delivery" class="item-10">
              <el-switch on-text="" off-text=""size="small" v-model="ruleForm.delivery"></el-switch>
            </el-form-item>
            <el-form-item label-width="100px" class="text-right item-10">
              <el-button type="primary" size="small" @click="submitForm('ruleForm')" >搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <!--列表搜索 复制-->

    <!--列表 复制-->
    <el-table
      ref="multipleTable"
      :data="tableData"
      stripe
      border
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="日期"
        width="120">
        <template scope="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="120">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        width="90">
        <template scope="scope">
          <el-switch
            v-model="scope.row.state"
            on-text=""
            off-text=""
            on-value="1"
            off-value="0">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <!--列表 复制-->

    <!--分页 复制-->
    <div class="page-wrap">
      <page :page-list="pageData" @pagesearch="init"></page>
    </div>
    <!--分页 复制-->

    <p class="mt20">特殊列表查看：http://element.eleme.io/#/zh-CN/component/table</p>
  </panel>
</template>

<script>
  import page from '@/components/page'

  export default {
    components:{page},
    data() {
      return {
        tableData: [{
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '0'
        }, {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '1'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '0'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '0'
        }, {
          date: '2016-05-08',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '1'
        }, {
          date: '2016-05-06',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '0'
        }, {
          date: '2016-05-07',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          state: '0'
        }],

        /**
         *表格选中集合
         */
        multipleSelection: [],

        /**
         * 重点搜索
         */
        input: '',

        /**
         * 高级搜索开关
         */
        searchState: false,

        /**
         * form表单对象
         */
        ruleForm: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },

        /**
         * 分页模拟数据 严格参照参数名
         */
        pageData: {
          list: [1,2,3,4,5,6,7,8],
          pageNumber: 1,
          pageSize: 5,
          totalCount: 8,
          totalPage: 1
        }
      }
    },
    mounted(){
      var page = {
        pageNumber: 1,
        pageSize: 5
      };
      /**
       * 默认获取数据 并 赋值给 pageData
       */
      this.init(page.pageNumber,page.pageSize);
    },
    methods: {
      /**
       * 重点搜索
       */
      handleIconClick(ev) {
        console.log(ev);
      },

      /**
       * 高级搜索
       */
      submitForm(formName) {
          console.log('submit!');
      },

      /**
       * 初始化表格 / 分页搜索 事件
       */
      init(number,size){
        console.log(number,size);
      },

      /**
       * 表格选择事件
       */
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

      /**
       * 导出excel 事件
       */
      handleDownload() {
        require.ensure([], () => {
          const { export_json_to_excel } = require('../../vendor/Export2Excel');
          const tHeader = ['日期', '姓名', '地址', '状态'];
          const filterVal = ['date', 'name', 'address', 'state'];
          const list = this.multipleSelection;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, '列表excel');
          this.$refs.multipleTable.clearSelection();
        })
      },

      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      }
    }
  }
</script>

<style>

</style>
